<?php if (!defined("_ECRIRE_INC_VERSION")) return; if (class_exists('AlohacartPaymentInvoice')) return;
/**
 * Aloha Cart
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage alohacart
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/AlohaCart
 */

include_spip('functions/pdk_common');
pdk_load_class('AlohacartExtension', 'classes'); 
 
/** 
 *  This class provides invoice payment logic
 *
 *  @author Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 */
class AlohacartPaymentInvoice extends AlohacartExtension {
    public $fields = array('supplier', 'show_payer', 'is_vat_payer', 'vat_type', 'vat_value');
    
    /**
     *  AlohacartOrderModel
     */
    //private $order_mdl;
	
	/**
	 * Invoice creator (IInvoiceCreator) that will create invoice
	 */
	//public IInvoiceCreator $InvoiceCreator;
    
    public function __construct() {
        parent::__construct();
        $this->configExtension();
        
        //$this->order_mdl = pdk_load_class('AlohacartOrderModel', 'classes');
		//$this->InvoiceCreator = pdk_load_class('InvoiceCreatorExcel', 'classes/payment/invoice/');
    }
    
    private function configExtension(){
        $this->type = 'payment';
        $this->code = 'invoice';
        
        // Define validation rules:         
         $this->validation->set_rules( 
            array(
               array(
                     'field'   => 'supplier',
                     'label'   => _T('alohacart_payment_invoice:supplier'),
                     'rules'   => 'required'
                  ),
            )
         );
    }
    
    public function GetData($options=array()) {
		// Try to get id_order from $options array or request
        if ($options['id_order']) $id_order = $options['id_order'];
        if (!$id_order && _request('id_order')) $id_order = _request('id_order');
        if (!$id_order) return '';
	
		$invoiceCreator = pdk_load_class('InvoiceCreatorExcel', 'classes/payment/invoice/'); 		
		if ($id_order) {
			mt_srand();
			$filename = md5(uniqid(mt_rand())).'.xls';
			$invoiceCreator->GenerateInvoice($id_order, $filename);			
			if (file_exists(_DIR_TMP.$filename)) {
				$GLOBALS['uploaded_files_data_alohacart_cart_order'][] = array('full_path'=>_DIR_TMP.$filename, 'orig_name'=>'invoice.xls','delete_after_use'=>true);
			}
		}
        return _T('alohacart_payment_invoice:invoice_will_be_sent_on_email');
    }    
}
?>